;; avoid using emacs bulti-in buffer for clipboard
(setq x-select-enable-clipboard t)
;; disable toolbar
(tool-bar-mode -1)
(menu-bar-mode -1)

(global-company-mode t)

(add-to-list 'load-path "~/.emacs.d/elisp/")
(require 'openbsd-knf-style)
(c-add-style "OpenBSD" openbsd-knf-style)
(setq c-default-style '((c-mode . "OpenBSD")))

;; Prohibit generating backup files and auto save files in the current directory

(setq backup-directory-alist
      `((".*" . ,temporary-file-directory)))
(setq auto-save-file-name-transforms
      `((".*" ,temporary-file-directory t)))
(setq backup-directory-alist '((".*" . "~/.Trash")))

;; Chinese Mirror for Package Archives

(setq package-archives '(("gnu" . "https://mirrors.ustc.edu.cn/elpa/gnu/")
                         ("melpa" . "https://mirrors.ustc.edu.cn/elpa/melpa/")
                         ("nongnu". "https://mirrors.ustc.edu.cn/elpa/nongnu/")))

;; General Text Mode, 78 columns per line, highlight line break

(defun my-general-text-mode ()
  (setq fill-column 78)
  (display-fill-column-indicator-mode 1)
  (turn-on-auto-fill))

(defun my-general-c-mode ()
  (my-general-text-mode 1)
  (line-number-mode 1)
  (column-number-mode 1)
  (auto-compression-mode 1)

  (setq search-highlight 1)
  (setq query-replace-highlight 1)
  (setq show-paren-delay 0
	show-paren-style 'parenthesis)
  (setq blink-matching-paren t)
  (transient-mark-mode 1)
  (show-paren-mode 1))

;; In the modeline, show which function you are currently in.
(add-hook 'c-mode-common-hook
	  (lambda ()
	    (when (derived-mode-p 'c-mode 'c++-mode 'java-mode)
	      (which-func-mode 1))))

;; Dired shows all files by default.  Use dired-x to stop displaying junk.
(add-hook 'dired-load-hook
          (lambda ()
            (load "dired-x")
            ;; I like to use '..' to navigate with dired so omit it here.
            (setq dired-omit-files "^\\.?#\\|^\\.$")
          ))
(add-hook 'dired-mode-hook
          (lambda ()
            (dired-omit-mode 1)
          ))

(add-hook 'text-mode-hook 'my-general-text-mode)
(add-hook 'python-mode-hook 'my-general-text-mode)
(add-hook 'org-mode-hook 'my-general-text-mode)
(add-hook 'c-mode-common-hook 'my-general-text-mode)
(add-hook 'asm-mode-hook 'my-general-text-mode)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; key bindings

(defun comment-or-uncomment-region-or-line ()
    (interactive)
    (let (beg end)
        (if (region-active-p)
            (setq beg (region-beginning) end (region-end))
            (setq beg (line-beginning-position) end (line-end-position)))
        (comment-or-uncomment-region beg end)))
(global-set-key (kbd "C-j") 'comment-or-uncomment-region-or-line)

(global-set-key (kbd "C-t") 'align-current)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; org-mode specific configurations

(global-set-key (kbd "C-c l") #'org-store-link)
(global-set-key (kbd "C-c a") #'org-agenda)
(global-set-key (kbd "C-c c") #'org-capture)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(custom-enabled-themes '(wheatgrass))
 '(menu-bar-mode nil)
 '(package-selected-packages
   '(company yasnippet sphinx-doc projectile org lsp-mode auto-complete))
 '(tool-bar-mode nil)
 '(warning-suppress-types '((comp))))
(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(default ((t (:family "Iosevka Term Slab" :foundry "UKWN" :slant normal :weight bold :height 158 :width normal)))))
